package com.qianfeng.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Role;
import com.qianfeng.mapper.RoleMapper;
import com.qianfeng.service.RoleService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Transactional()
    @Override
    public void addRole(Role role) {
        roleMapper.addRole(role);
    }

    @Transactional
    @Override
    public void deleteRole(Integer roleId) {
        roleMapper.deleteRole(roleId);
    }

    @Transactional
    @Override
    public void updateRole(Role role) {
        roleMapper.updateRole(role);
    }


    @Transactional(readOnly = true)
    @Override
    public Role getRoleByRoleId(Integer roleId) {
        return roleMapper.getRoleByRoleId(roleId);
    }

    @Transactional(readOnly = true)
    @Override
    public Page getRolePage(Integer pageNo, Integer pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<Role> roleList = roleMapper.getRoleList();
        PageInfo<Role> info = new PageInfo<>(roleList);
        page.setPage(info.getList());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setPageCount(info.getPages());
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        return page;
    }
}
